package com.example.demo;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    private NewsFragmentAdapter newsFragmentAdapter;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private TextView tv5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFragmentList();

        initViews();
    }

    private void initFragmentList() {
        NewsFragment newsFragment1 = new NewsFragment();
        newsFragment1.setType("top");
        NewsFragment newsFragment2 = new NewsFragment();
        newsFragment2.setType("yule");
        NewsFragment newsFragment3 = new NewsFragment();
        newsFragment3.setType("tiyu");
        NewsFragment newsFragment4 = new NewsFragment();
        newsFragment4.setType("keji");
        NewsFragment newsFragment5 = new NewsFragment();
        newsFragment5.setType("shehui");

        fragmentList.add(newsFragment1);
        fragmentList.add(newsFragment2);
        fragmentList.add(newsFragment3);
        fragmentList.add(newsFragment4);
        fragmentList.add(newsFragment5);

    }

    private void initViews() {
        tv1=findViewById(R.id.tv_1);
        tv2=findViewById(R.id.tv_2);
        tv3=findViewById(R.id.tv_3);
        tv4=findViewById(R.id.tv_4);
        tv5=findViewById(R.id.tv_5);
        viewPager = findViewById(R.id.news_viewPager);
        viewPager.setOffscreenPageLimit(6);
        newsFragmentAdapter =  new NewsFragmentAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(newsFragmentAdapter);
        tv1.setBackgroundColor(Color.RED);

        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
            }
        });
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
            }
        });
        tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2);
            }
        });
        tv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(3);
            }
        });
        tv5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(4);
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position){
                    case 0:
                        tv1.setBackgroundColor(Color.RED);
                        tv2.setBackgroundColor(Color.TRANSPARENT);
                        tv3.setBackgroundColor(Color.TRANSPARENT);
                        tv4.setBackgroundColor(Color.TRANSPARENT);
                        tv5.setBackgroundColor(Color.TRANSPARENT);
                        break;
                    case 1:
                        tv1.setBackgroundColor(Color.TRANSPARENT);
                        tv2.setBackgroundColor(Color.RED);
                        tv3.setBackgroundColor(Color.TRANSPARENT);
                        tv4.setBackgroundColor(Color.TRANSPARENT);
                        tv5.setBackgroundColor(Color.TRANSPARENT);
                        break;
                    case 2:
                        tv1.setBackgroundColor(Color.TRANSPARENT);
                        tv2.setBackgroundColor(Color.TRANSPARENT);
                        tv3.setBackgroundColor(Color.RED);
                        tv4.setBackgroundColor(Color.TRANSPARENT);
                        tv5.setBackgroundColor(Color.TRANSPARENT);
                        break;
                    case 3:
                        tv1.setBackgroundColor(Color.TRANSPARENT);
                        tv2.setBackgroundColor(Color.TRANSPARENT);
                        tv3.setBackgroundColor(Color.TRANSPARENT);
                        tv4.setBackgroundColor(Color.RED);
                        tv5.setBackgroundColor(Color.TRANSPARENT);
                        break;
                    case 4:
                        tv1.setBackgroundColor(Color.TRANSPARENT);
                        tv2.setBackgroundColor(Color.TRANSPARENT);
                        tv3.setBackgroundColor(Color.TRANSPARENT);
                        tv4.setBackgroundColor(Color.TRANSPARENT);
                        tv5.setBackgroundColor(Color.RED);
                        break;
                }
            }
        });
    }
}
